﻿using System;
using System.Web.UI;
using MasterPageControlExample.Controls;

namespace MasterPageControlExample
{
    public partial class SiteMaster : MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void DisplayHeader(bool value)
        {
            if(Header1!=null)
                Header1.Visible = value;
            else
            {
                Page.LoadControl(@"Controls\Header.ascx");
                var cachingControl = PlaceHolderHeader.Controls[0] as StaticPartialCachingControl;
                if (cachingControl != null) cachingControl.Visible = value;
            }
        }

        public void SetSiteTitle(string siteTitle)
        {
            if (Header1 != null)
                Header1.SiteTitle = siteTitle;
            else
            {
                var cachedHeader = (PartialCachingControl)LoadControl(@"Controls\Header.ascx");
                PlaceHolderHeader.Controls.Clear();
                PlaceHolderHeader.Controls.Add(cachedHeader);
                if (cachedHeader.CachedControl != null)
                {
                    var header = cachedHeader.CachedControl as Header;
                    if (header != null) header.SiteTitle = siteTitle;
                }
            }
        }

        public void DisplayFooter(bool value)
        {
            if (Footer1 != null)
                Footer1.Visible = value;
            else
            {
                LoadControl(@"Controls\Footer.ascx");
                var cachingControl = PlaceHolderFooter.Controls[0] as StaticPartialCachingControl;
                if (cachingControl != null) cachingControl.Visible = value;    
            }
        }
    }
}
